package com.jiaomatech.tencent.cos.helper;

import com.jiaomatech.tencent.cos.CosProperties;
import com.qcloud.cos.model.PutObjectResult;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

/**
 * Description: COS 操作类 .<br>
 *
 * @author jm
 *     Created By 2018/9/30 上午10:52
 */
public class CosHelper {
    /**
     * https://kmh-1256033600.cos.ap-beijing.myqcloud.com/checkstyle.xml .
     */
    private static final String URL = "%s://%s.cos.%s.myqcloud.com/%s";

    @Autowired
    private CosProperties cosProperties;

    /**
     * 处理最后结果.
     *
     * @param putObjectResult .
     * @return .
     */
    public String getUrl(PutObjectResult putObjectResult, String bucketName, String key) {
        if (Objects.nonNull(putObjectResult) && Objects.nonNull(putObjectResult.getRequestId())) {
            return String.format(URL,
                cosProperties.getClientConfig().getHttpProtocol().toString(),
                bucketName,
                cosProperties.getClientConfig().getRegion().getRegionName(),
                key
            );
        }
        return "";
    }
}
